import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Properties;

public class SendEmail2 {
    //javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
    //nested exception is:
    //java.net.ConnectException: Connection refused: connect
    //要求用户名，密码，以及其他邮箱验证信息
    public static void main(String[] args) {
        // 收件人电子邮箱
        String to = "582956590@qq.com";

        // 发件人电子邮箱
        String from = "3365582964@qq.com";

        // 指定发送邮件的主机为 localhost
        //String host = "localhost";
        // 指定发送邮件的主机为 smtp.qq.com
        String host = "smtp.qq.com";  //QQ 邮件服务器

        // 获取系统属性
        Properties properties = System.getProperties();

        // 设置邮件服务器
        properties.setProperty("mail.smtp.host", host);

        //如果需要提供用户名和密码给e-mail服务器来达到用户认证的目的，你可以通过如下设置来完成
        //properties.put("mail.smtp.auth", "true");
        //properties.setProperty("mail.user", "yourUser");
        //properties.setProperty("mail.password", "yourPwd");


        properties.put("mail.smtp.auth", "true");
        // 获取默认session对象
        //Session session = Session.getDefaultInstance(properties);
        //需要授权码的话//发件人邮件用户名、授权码
        Session session = Session.getDefaultInstance(properties, new Authenticator() {
                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        //return super.getPasswordAuthentication();
                        return new PasswordAuthentication(from, "tuptvaemqlsidbad"); //发件人邮件用户名、授权码
                    }
                }

        );
        try {
            // 创建默认的 MimeMessage 对象
            MimeMessage message = new MimeMessage(session);

            // Set From: 头部头字段
            message.setFrom(new InternetAddress(from));

            // Set To: 头部头字段
            //发送一封e-mail给多个收件人，那么使用下面的方法来指定多个收件人ID：
            //void addRecipients(Message.RecipientType type,Address[] addresses)
            //后面的参数用数组
            //type:要被设置为 TO, CC 或者 BCC，这里 CC 代表抄送、BCC 代表秘密抄送
            message.addRecipient(Message.RecipientType.TO,
                    new InternetAddress(to));

            // Set Subject: 头部头字段
            message.setSubject("This is the Subject Line!");
// 设置消息体
            message.setText("This is actual message");
            // 创建消息部分
            BodyPart messageBodyPart  = new MimeBodyPart();

            // 消息
            messageBodyPart.setText("This is message body");

            // 创建多重消息
            Multipart multipart = new MimeMultipart();

            // 附件部分
                    messageBodyPart = new MimeBodyPart();
            String filename = "ASCII码对照表.png.jpg";
            DataSource source = new FileDataSource(filename);
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName(filename);
            multipart.addBodyPart(messageBodyPart);

            //message.setContent("<h1>This is test message</h1>", "text/html");
            message.setContent(multipart);
            // 发送消息
            Transport.send(message);
            System.out.println("Sent message successfully....");
        } catch (MessagingException mex) {
            mex.printStackTrace();
        }


    }
}
